package com.coded2.mobileposto.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.coded2.infra.comum.ComumDAO;
import com.coded2.mobileposto.model.PostoCombustivel;

public class PostoCombustivelDAO extends ComumDAO<PostoCombustivel> {

	private static final long serialVersionUID = 1L;

	@Override
	protected List<Predicate> searchConditions(PostoCombustivel obj,
			CriteriaBuilder builder, Root<PostoCombustivel> root) {
		List<Predicate> conditions = new ArrayList<Predicate>();
		Predicate idPosto = builder.equal(root.get("idPosto"), obj.getPosto().getId());
		conditions.add(idPosto);
		return conditions;
	}

	public List<PostoCombustivel> searchByPosto(PostoCombustivel pc) {
		String sql="SELECT `posto_combustivel`.`id_combustivel`, `posto_combustivel`.`id_posto`, `posto_combustivel`.`preco` "
				 + "FROM `mobile_posto_admin`.`posto_combustivel`"
				 + "where `posto_combustivel`.`id_posto` = ?;";
		Query query = em.createNativeQuery(sql,PostoCombustivel.class);
		int index=1;
		query.setParameter(index++, pc.getPosto().getIdPosto());
		return query.getResultList();
	}

}
